/*
Copyright (C) 2014 Yuvraj Singh Babrah

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package design;

import javax.swing.JPanel;

/**
 *
 * @author Yuvraj Singh Babrah
 */
public class SingleDownloadPanel extends javax.swing.JPanel {
    /* props */
    private int downloaded = 0;
    private int total = 0;
    private String albumName = null;
    
    /**
     * Creates new form SingleDownloadPanel
     */
    public SingleDownloadPanel() {
        initComponents();
        customChanges();
    }
    
    /**
     * customChanges
     */
    public void customChanges() {
        progressBar.setMinimum(0);
    }
    
    /**
     * updateTopLabel
     * @param topLabel 
     */
    public void updateTopLabel(String topLabel) {
        mainInfo.setText(topLabel);
        albumName = topLabel.substring(0, topLabel.indexOf("-"));
    }
    
    /**
     * updateBottomLabel
     * @param bottomLabel 
     */
    public void updateBottomLabel(String bottomLabel) {
        String split[] = bottomLabel.split("/");
        downloaded = Integer.parseInt(split[0]);
        total = Integer.parseInt(split[1]);
        count.setText(bottomLabel);
    }
    
    /**
     * updateProgressBar
     * @param count 
     */
    public synchronized void updateProgressBar() {
        downloaded ++;
        progressBar.setValue(downloaded);
        count.setText(downloaded + "/" + total); /* update bottom label */
        /* check for completion */
        if(downloaded >= total) {
            DownloadsFrame.removePanel(this, total, albumName);
        }
    }
    
    /**
     * setLimit
     * @param count 
     */
    public void setLimit(int count) {
        progressBar.setMaximum(count);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainInfo = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        count = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 153, 255)));
        setEnabled(false);

        mainInfo.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        mainInfo.setForeground(new java.awt.Color(102, 102, 102));
        mainInfo.setText("New York City -  Yuvraj Singh Babrah");

        count.setFont(new java.awt.Font("Segoe UI", 1, 11)); // NOI18N
        count.setForeground(new java.awt.Color(153, 153, 153));
        count.setText("12/183");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(count)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(mainInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 941, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainInfo)
                .addGap(6, 6, 6)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(count)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel count;
    private javax.swing.JLabel mainInfo;
    private javax.swing.JProgressBar progressBar;
    // End of variables declaration//GEN-END:variables
}
